<?
 require_once("functions.php");
 
function AddEmail($new_email_array)
{
	$database = getDB();
	
	$new_email_array = arrayEscape($new_email_array, $database);

	$statement = "INSERT INTO Email (address, Contact_type_id, Customer_Name_id) VALUES ('$new_email_array[address]', '$new_email_array[contact_type]', '$new_email_array[id]')";

	$results = $database->query($statement);

	$error = $database->error;
	
	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error);
}

function delEmail($emailId)
{
	$database = getDB();
	
	$new_email_array = arrayEscape($new_email_array, $database);

	$statement = "DELETE FROM Email WHERE id = '$emailId'";

	$results = $database->query($statement);

	$error = $database->error;
	
	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error);
}

function getEmailEntry($emailId)
{
	$database = getDB();

	$statement = " SELECT Email.id AS id, address, Contact_type_id, value AS Contact, Customer_Name_id AS Customer FROM Email LEFT JOIN Keyword ON Contact_type_id = Keyword.id WHERE Email.id = '$emailId'";

	$results = $database->query($statement);
	
	$database->close();
	
	return $results;
}

function getUserEmails($Customer_id)
{
	$database = getDB();
	
	$statement = "SELECT Email.id AS id, address, value AS Contact FROM Email LEFT JOIN Keyword ON Contact_type_id = Keyword.id WHERE Customer_Name_id = '$Customer_id'";

	$results = $database->query($statement);

	$database->close();
	
	return $results;
}


function updateEmail($update_email_array)
{
	$database = getDB();
	
	$update_email_array = arrayEscape($update_email_array, $database);

	$statement = "UPDATE Email SET address = '$update_email_array[Email]', Contact_type_id = '$update_email_array[contact_type_id]', Customer_Name_id = '$update_email_array[Customer_id]' WHERE id = '$update_email_array[id]'";
 
 	print($statement);
 
	$results = $database->query($statement);

	$error = $database->error;
	
	$database->close();
	
	return $error == "" ? true : array('SQL Error'=> $error, 'statement' => $statement);
}

function getEmailTypes()
{
	$database = getDB();

	$statement = "SELECT id, value AS Type FROM Keyword WHERE keyword_types = '4'";

	$results = $database->query($statement);
	
	$database->close();
	
	return $results;
}


?>